﻿using CRM.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CRM.Controllers.Customer
{
    using CustomerModel = CRM.Models.Customer;

    [Route("api/[controller]")]
    [ApiController]
    public class CustomerHomeController : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly UserManager<User> _userManager;
        public CustomerHomeController(MyDbContext dbContext, UserManager<User> userManager)
        {
            _dbContext = dbContext;
            _userManager = userManager;
        }

        [HttpGet(nameof(Workbench))]
        public async Task<ResultStruct> Workbench()
        {
            var user = await _userManager.GetUserAsync(User);

            var list = await _dbContext.Customer.Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.UserId == user.Id)
               .OrderByDescending(m => m.Id)
               .Select(c => new { c.DealStatus, c.IsPrivate, c.UpdatedAt, c.FollowAt })
               .ToArrayAsync();
            //客户总数
            var customerCount = list.Length;
            //成交客户
            var dealCustomerCount = list.Count(m => m.DealStatus == 1);

            //7天内移入公海
            var sevenDaysAgo = DateTime.Now.AddDays(-7);
            var waterCustomerCount = list.Count(c => c.IsPrivate == 0 && c.UpdatedAt > sevenDaysAgo);

            //七天未跟进
            var sevenDaysNotFollow = list.Count(m => m.FollowAt < sevenDaysAgo);

            //15天未跟进
            var fifteenDaysAgo = DateTime.Now.AddDays(-15);
            var fifteenDaysNotFollow = list.Count(m => m.FollowAt < fifteenDaysAgo);

            //一个月未跟进
            var oneMonthAgo = DateTime.Now.AddMonths(-1);
            var oneMontNotFollow = list.Count(m => m.FollowAt < oneMonthAgo);

            //一年未跟进
            var oneYearAgo = DateTime.Now.AddYears(-1);
            var oneYearNotFollow = list.Count(m => m.FollowAt < oneYearAgo);

            //更久未跟进
            var longTimeNotFollow = customerCount - oneYearNotFollow;

            //已发营销邮件
            var emailCount = await _dbContext.EmailSendHistory
                .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId && m.UserId == user.Id)
                .CountAsync();

            return Success(new
            {
                CustomerCount = customerCount,
                DealCustomerCount = dealCustomerCount,
                WaterCustomerCount = waterCustomerCount,
                SevenDaysNotFollow = sevenDaysNotFollow,
                FifteenDaysNotFollow = fifteenDaysNotFollow,
                OneMontNotFollow = oneMontNotFollow,
                OneYearNotFollow = oneYearNotFollow,
                LongTimeNotFollow = longTimeNotFollow,
                EmailCount = emailCount
            });

        }
    }
}
